package com.example.javamaday.leetcode;

import java.util.ArrayList;
import java.util.Arrays;

public class LeetCodeTest0350 {

    public static void main(String[] args) {

    }

    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        ArrayList<Integer> result = new ArrayList<>();
        int index1 = 0;
        int index2 = 0;
        System.out.println(Arrays.deepToString(new int[][]{nums1}));
        System.out.println(Arrays.deepToString(new int[][]{nums2}));
        while (index1 < nums1.length && index2 < nums2.length) {
            if (nums1[index1] < nums2[index2]) {
                index1++;
                continue;
            }
            if (nums1[index1] > nums2[index2]) {
                index2++;
                continue;
            }
            if (nums1[index1] == nums2[index2]) {
                result.add(nums1[index1]);
                index1++;
                index2++;
                continue;
            }
        }
        int[] resultArray = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            resultArray[i] = result.get(i);
        }
        return resultArray;
    }
}
